<?php

/**
 * CoreControllerRuler
 * 
 * Description of I_Core_Controller_Rule
 * 
 * @package Core
 * @subpackage Controllers
 * @category Controllers
 * @author cjc
 * @version 1.0
 */
class I_Core_Controller_Rule extends CI_Controller {
    /**
     *
     * @var string $folder set folder template
     */
    protected $folder = '';

    /**
     * @var mixed[] $data set var for views; 
     */
    public $data = array();

    /**
     * @var mixed[] $files_used  is container files as used 
     */
    private $files_used = array();

    /**
     * @var mixed[] $files_includes array() 
     */
    private $files_includes = array(
        'js' => array(
            'header' => array(),
            'footer' => array()
        ),
        'css' => array(
            'header' => array(),
            'footer' => array()
        )
    );
    /**
     * @name I_Core_Controller_Rule
     */
    public function __construct() {
        parent::__construct();
        
        
        $this->twig->addFilter('add_css');
        $this->twig->addFilter('add_js');
        $this->twig->addFilter('add_javascript', function($file, $pos = 'footer') {
            $this->add_js($file, $pos);
        });
        $this->files_includes['js']['header'][] = '<script type="text/javascript"> var base_url = "' . $this->config->item('base_web') . '";</script>';
    }

    /**
     * render
     * 
     * @access public
     * @param  string $name
     * @return object
     */
    function render($name = '') {

        $this->data['js'] = $this->files_includes['js'];
        $this->data['css'] = $this->files_includes['css'];
        $this->twig->render($this->folder . $name, $this->data, FALSE);
        return $this->twig;
    }

    /**
     * display
     * 
     * @param string $name
     * @return instance
     */
    function display($name = '') {
        $this->data['js'] = $this->get_js_files();
        $this->data['css'] = $this->files_includes['css'];
        $this->twig->render($this->folder . $name, $this->data);
        return $this->twig;
    }

    /**
     * add_js
     * 
     * @param string $file
     * @param string $position
     */
    function add_js($file, $position = 'header') {
        $position = $position == 'header' ? 'header' : 'footer';
        $file = $this->clear_ext_file($file) . '.js';

        if (!in_array($file, $this->files_includes['js'][$position]) && !in_array(strtolower($file), $this->files_used)) {

            $this->files_includes['js'][$position][] = '<script src="' . $this->config->item('web_dir') . $this->config->item('js_dir') . $file . '" type="text/javascript"></script>';
            $this->files_used[] = strtolower($file);
        }
    }

    /**
     * add_css
     * 
     * @param string $file
     * @param string $position
     */
    function add_css($file, $position = 'header') {
        $position = $position == 'header' ? 'header' : 'footer';

        $file = $this->clear_ext_file($file) . '.css';
        if (!in_array($file, $this->files_includes['css'][$position]) && !in_array(strtolower($file), $this->files_used)) {
            $this->files_includes['css'][$position][] = '<link href="' . $this->config->item('web_dir') . $this->config->item('css_dir') . $file . '" rel="stylesheet"/>';
            $this->files_used[] = strtolower($file);
        }
    }

    /**
     * get_js_files
     * 
     * @return mixed[]
     */
    private function get_js_files() {

        return $this->files_includes['js'];
    }

    /**
     * clear_ext_file
     * 
     * @param string $file
     * @return string
     */
    private function clear_ext_file($file) {
        $array_extensions = array('.js', '.css');
        $a_replace = array();
        return str_replace($array_extensions, $a_replace, $file);
    }

    /**
     * view
     * 
     * @param string|mixed[] $view
     */
    function view($view) {
        $this->load->view($this->config->item('themes') . $this->folder . $view, $this->data);
    }

}
